package com.wyj.aop;
import lombok.extern.log4j.Log4j2;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Log4j2
@Component
public class JpaMethodAop {
    @Pointcut("execution(* com.wyj.dao.UserDao.findGameUsersByUserName(..))")
    public void pointJpa(){
    }

    @Around("pointJpa()")
    public Object aroundJpa(ProceedingJoinPoint pjp) throws Throwable {
        log.info("around");
        Long start = System.currentTimeMillis();
        //调用目标方法 我们的这个环绕才起作用。
        Object proceed = pjp.proceed();
        Long end = System.currentTimeMillis();
        log.info("around后"+(end-start)/1000+"秒");
        return proceed;
    }

    //在切点前执行以下代码
    @Before("pointJpa()")
    public void beforeJpa(){
        System.out.println("前置通知");
    }

    //不管目标方法有无异常，都会执行@After注解方法的业务逻辑）
    @After("pointJpa()")
    public void afterJpa(){
        System.out.println("后置通知");
    }

    //（若目标方法无异常，执行@AfterReturning注解方法的业务逻辑）
    @AfterReturning("pointJpa()")
    public void afterReturn(){
        System.out.println("后置返回 ");
    }

    //（若目标方法有异常，执行@AfterThrowing注解方法的业务逻辑）
    @AfterThrowing("pointJpa()")
    public void afterThrow(){
        System.out.println("后置异常");
    }
}
